package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.dao.PaperItemDao;
import com.woniuxy.dao.QuestionInventoryMapper;
import com.woniuxy.dao.TestDao;
import com.woniuxy.entity.PaperItem;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Test;
import com.woniuxy.service.TestService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

import java.util.*;


@Service
public class TestServiceImpl implements TestService {

    @Autowired
    TestDao testDao;
    @Autowired
    QuestionInventoryMapper questionInventoryDao;

    @Autowired
    PaperItemDao paperItemDao;

    @Override
    public List<Test> findAllTest(Map map) {
        return testDao.findAllTest(map);
//        return new ArrayList<>();
    }

    @Override
    public ResponseEntity addTest(String json) {
        Test test = new Test();

        JSONObject jsonObject = JSON.parseObject(json);
        Integer user = jsonObject.getInteger("user");
        String sub = jsonObject.getString("sub");
        String name = jsonObject.getString("name");
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Integer exclusive = jsonObject.getInteger("exclusive");
        Integer multiple = jsonObject.getInteger("multiple");
        Integer read = jsonObject.getInteger("read");
        String classs = StringUtils.strip(jsonObject.getString("classs"),"[]");
        System.out.println(classs);
        String doTime = jsonObject.getString("doTime");
        String des = jsonObject.getString("des");
        Integer paperId = jsonObject.getInteger("paperId");
        String optOrder = jsonObject.getString("optOrder");
        String topOrder = jsonObject.getString("topOrder");
        String okScore = jsonObject.getString("okScore");



        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String code=df.format(date);
        String code1=code.replaceAll("-","");
        String code2=code1.replaceAll(" ","");
        String code3=code2.replaceAll(":","");

        test.setCode(code3);
        test.setName(name);
        Integer id=questionInventoryDao.findIdBySub(sub);
        //System.out.println(id);
        test.setSubjectId(id);
        test.setTeacherId(user);

//        String classs1=classs.replaceAll("]","");
//        String classs2=classs1.replaceAll("[","");
//        System.out.println(classs2);
        test.setClassId(classs);
        test.setStartTime(startTime);
        test.setEndTime(endTime);
        test.setExclusive(exclusive);
        test.setMultiple(multiple);
        test.setRead(read);
        test.setClassId(classs);
        test.setDoTime(doTime);
        test.setDes(des);
        test.setPaperId(paperId);
        test.setOptOrder(optOrder);
        test.setTopOrder(topOrder);
        test.setOkScore(okScore);
        test.setStatus("n");



        int i = testDao.addTest(test);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }

    }

    @Override
    public List<PaperItem> paperFindAllTest() {
        return paperItemDao.findAll();
    }

    @Override
    public ResponseEntity delTest(Integer id) {
       int i= testDao.delTest(id);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public ResponseEntity update(String json) {
        Test test = new Test();
        JSONObject jsonObject = JSON.parseObject(json);
        Integer id = jsonObject.getInteger("id");
        String status=jsonObject.getString("status");
        test.setId(id);
        test.setStatus(status);


        int i= testDao.update(test);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public Test findOnesTest(Map map) {
        Integer userId=(Integer) map.get("id");

        return testDao.findOnesTest(userId);
    }

    @Override
    public List<Test> findAllOnesTest(Map map) {
        Integer userId=(Integer) map.get("userId");
        String testName = (String) map.get("testName");
        Map<String ,Object> map1=new HashMap<>();
        map1.put("userId",userId);
        map1.put("testName",testName);
        return testDao.findAllOnesTest(map1);
    }
}
